package com.taptech.doufu.bean.novel;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import java.lang.reflect.Type;

/**
 * Created by Wyatt on 2017/9/18/018.
 */

public class PayInfoBeanDeserializer implements JsonDeserializer<PayInfoBean>{
    @Override
    public PayInfoBean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        PayInfoBean bean = new PayInfoBean();
        if (json.isJsonObject()) {
            JsonObject jsonObject = json.getAsJsonObject();
            if (jsonObject.has("discount")) {
                bean.setDiscount(jsonObject.get("discount").getAsFloat());
            }
            if (jsonObject.has("is_vip")) {
                bean.setIs_vip(jsonObject.get("is_vip").getAsBoolean());
            }
            if (jsonObject.has("isNewVip")) {
                bean.setNewVip(jsonObject.get("isNewVip").getAsBoolean());
            }
            if(jsonObject.has("surplusTotal")){
                bean.setSurplusTotal(jsonObject.get("surplusTotal").getAsInt());
            }
            if(jsonObject.has("fullbuyPrice")){
                String priceStr = jsonObject.get("fullbuyPrice").getAsString();
                try {
                    float priceInt = Float.parseFloat(priceStr);
                    bean.setFullbuyPrice((int)priceInt);
                } catch (Exception e) {
                }
            }
            if (jsonObject.has("purchasedTotalHalf")) {
                bean.setPurchasedTotalHalf(jsonObject.get("purchasedTotalHalf").getAsInt());
            }

            if (jsonObject.has("fullBuyNote")){
                bean.setFullBuyNote(jsonObject.get("fullBuyNote").getAsString());
            }

            if (jsonObject.has("ticketCountInfo")){
                try {
                    PayInfoBean.TicketCountInfo info = new PayInfoBean.TicketCountInfo();
                    JsonObject infoObject = jsonObject.getAsJsonObject("ticketCountInfo");

                    if (infoObject.has("official_used")){
                        info.setOfficial_used(infoObject.get("official_used").getAsInt());
                    }
                    if (infoObject.has("official_usable")){
                        info.setOfficial_usable(infoObject.get("official_usable").getAsInt());
                    }

                    if (infoObject.has("author_give_used")){
                        info.setAuthor_give_used(infoObject.get("author_give_used").getAsInt());
                    }

                    if (infoObject.has("author_give_usable")){
                        info.setAuthor_give_usable(infoObject.get("author_give_usable").getAsInt());
                    }

                    if (infoObject.has("show_dialog")){
                        info.setShow_dialog(infoObject.get("show_dialog").getAsBoolean());
                    }
                    bean.setTicketCountInfo(info);
                }catch (Exception e){

                }
            }

            if (jsonObject.has("readTicketInfo")) {
                try {
                    PayInfoBean.ReadTicketInfo info = new PayInfoBean.ReadTicketInfo();
                    JsonObject ticketObject = jsonObject.getAsJsonObject("readTicketInfo");

                    if (ticketObject.has("id")&& ticketObject.get("id")!=null) {
                        info.setId(ticketObject.get("id").getAsInt());
                    }
//                    if (ticketObject.has("object_type")&&ticketObject.get("object_type")!=null) {
//                        info.setObject_type(ticketObject.get("object_type").getAsInt());
//
//                    }
                    if (ticketObject.has("ticket_type")&&ticketObject.get("ticket_type")!=null) {
                        info.setTicket_type(ticketObject.get("ticket_type").getAsInt());

                    }
                    if (ticketObject.has("ticket_type_txt")&&ticketObject.get("ticket_type_txt")!=null) {
                        info.setTicket_type_txt(ticketObject.get("ticket_type_txt").getAsString());

                    }
                    bean.setReadTicketInfo(info);
                } catch (Exception e) {
                }
            }
        }
        return bean;
    }
}
